package com.heer.gymnasium.module.card.views;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;

import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseRefreshActivity;
import com.heer.gymnasium.constant.Type;
import com.heer.gymnasium.module.card.beans.CardListResponseBean;
import com.heer.gymnasium.module.card.beans.MyCardBean;
import com.heer.gymnasium.module.main.views.MainActivity;
import com.heer.gymnasium.module.order.views.OrderActivity;
import com.heer.gymnasium.util.StringUtils;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.zhihuiyun.library.commonadapter.QuickAdapter;
import com.zhihuiyun.library.commonadapter.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 购卡
 * Created by zy on 2018/1/25.
 */

public class BuyCardListActivity extends BaseRefreshActivity {

    private final static int RES_ID_ROOT = R.layout.layout_purchase;

    private final static int RES_ID_LIST = R.id.layout_purchase_lv;

    private final static int RES_ID_LIST_ITEM = R.layout.item_card;
    private final static int RES_ID_STORE_NAME = R.id.item_card_store_name_tv;
    private final static int RES_ID_STORE_NAME_IV = R.id.item_card_store_name_iv;
    private final static int RES_ID_TYPE = R.id.item_card_type_tv;
    private final static int RES_ID_STADIUM_NAME = R.id.item_card_stadium_name_tv;
    private final static int RES_ID_PLACE = R.id.item_card_place_tv;
    private final static int RES_ID_PERIOD = R.id.item_card_period_tv;
    private final static int RES_ID_TIMES = R.id.item_card_times_tv;
    private final static int RES_ID_CARD_NO = R.id.item_card_no_tv;
    private final static int RES_ID_BOTTOM_NAME = R.id.item_card_bottom_show_name_tv;
    private final static int RES_ID_PRICE = R.id.item_card_price_tv;
    private final static int[] RES_IDS_TV = new int[]{
            RES_ID_STORE_NAME,
            RES_ID_TYPE,
            R.id.item_card_use_tag_tv,
            RES_ID_STADIUM_NAME,
            RES_ID_PLACE,
            R.id.item_card_period_tag_tv,
            RES_ID_PERIOD,
            R.id.item_card_times_tag_tv,
            RES_ID_TIMES,
            RES_ID_CARD_NO,
            R.id.item_card_logo_tv,
    };
    private final static int RES_ID_PANEL = R.id.item_card_panel_ll;
    private final static int RES_ID_LINE = R.id.item_card_bottom_line_v;


    private final static int DRAWABLE_BG_BLACK = R.drawable.ic_purchase;
    private final static int DRAWABLE_BG_YELLOW = R.drawable.ic_card_yellowbg;
    private final static int DRAWABLE_ICON_WHITE = R.drawable.ic_card_white;
    private final static int DRAWABLE_ICON_BLACK = R.drawable.ic_card_gray;

    @BindView(RES_ID_LIST)
    ListView listView;

    private ArrayList<MyCardBean> dataArray;
    private QuickAdapter<MyCardBean> adapter;

    private Activity $this;

    @Override
    protected void init() {
        super.init();
        $this = this;
        setContentView(RES_ID_ROOT);
        setRefreshResId(RES_ID_REFRESH_LAYOUT);
        dataArray = new ArrayList<>();

        adapter = new QuickAdapter<MyCardBean>(this, RES_ID_LIST_ITEM, dataArray) {
            @Override
            public void bindData4View(ViewHolder holder, final MyCardBean data, int pos) {
                if (data.getNum().equals("0")) {
                    for (int i = 0; i < RES_IDS_TV.length; i++) {
                        holder.setTextColor(RES_IDS_TV[i], Color.BLACK);
                    }
                    holder.setBg(RES_ID_PANEL, DRAWABLE_BG_YELLOW);
                    holder.setImg(RES_ID_STORE_NAME_IV, DRAWABLE_ICON_BLACK);
                    holder.setText(RES_ID_TIMES, "无限次");

                } else {
                    for (int i = 0; i < RES_IDS_TV.length; i++) {
                        holder.setTextColor(RES_IDS_TV[i], Color.WHITE);
                    }
                    holder.setBg(RES_ID_PANEL, DRAWABLE_BG_BLACK);
                    holder.setImg(RES_ID_STORE_NAME_IV, DRAWABLE_ICON_WHITE);
                    holder.setText(RES_ID_TIMES, data.getNum() + "次");
                }
                if (pos >= getCount() - 1) {
                    holder.setVisibility(RES_ID_LINE, View.GONE);
                } else {
                    holder.setVisibility(RES_ID_LINE, View.VISIBLE);
                }

                holder.setText(RES_ID_STORE_NAME, data.getShow_name());
                holder.setText(RES_ID_TYPE, data.getCard_cat_name());
                holder.setText(RES_ID_STADIUM_NAME, data.getStadium_name());


                holder.setText(RES_ID_PLACE, getScope(data.getStadium_list()));
                holder.setText(RES_ID_PERIOD, data.getExpire() + "天");
                holder.setText(RES_ID_CARD_NO, data.getCard_no());
                holder.setText(RES_ID_BOTTOM_NAME, data.getShow_name());
                holder.setText(RES_ID_PRICE, "¥" + data.getPrice());

                holder.getParentView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        OrderActivity.startActivity($this, OrderActivity.OrderType.from_card, data.getCard_id(), Type.ORDER_CARD);
                    }
                });

            }
        };
    }

    private String getScope(List<MyCardBean.Stadium.Tag> tags) {
        String scope = "";
        if (tags == null) {
            return scope;
        }
        if (tags.get(0).getPlace().length == 1) {
            scope = tags.get(0).getPlace()[0];
        } else if (tags.get(0).getPlace().length >= 2) {
            String upPlace = tags.get(0).getPlace()[0].length() > 8 ? tags.get(0).getPlace()[0].substring(0, 7) + "..."
                    : tags.get(0).getPlace()[0];
            String downPlace = tags.get(0).getPlace()[1].length() > 8 ? tags.get(0).getPlace()[1].substring(0, 7) + "..."
                    : tags.get(0).getPlace()[1];
            scope = upPlace + "\n" + downPlace;
        }


        return scope;
    }

    @Override
    protected void initView() {
        super.initView();
        titleUtils.setTitleText("单人票");
        titleUtils.startMainActivity($this);
        listView.setAdapter(adapter);
    }

    @Override
    protected void loadData(boolean loadMore) {
        super.loadData(loadMore);
        requestData();
        if (layoutRefresh.isRefreshing()) {
            layoutRefresh.setRefreshing(false);
        }
    }

    //    请求网络数据
    private void requestData() {
        String id = this.getIntent().getStringExtra(EXTRA_ID);
        if (TextUtils.isEmpty(id)) {
            return;
        }
        mAppHelper.apiService().getCardList(id).enqueue(new BaseCallback<ApiResponse<CardListResponseBean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<CardListResponseBean>> call, Response<ApiResponse<CardListResponseBean>> response) {
                if (pageNo == 1) {
                    dataArray.clear();
                }
                int total = StringUtils.toInt(response.body().getData().getTotal());
                int current = StringUtils.toInt(response.body().getData().getCurrent_page());
                int per_page = StringUtils.toInt(response.body().getData().getPer_page());
                if (total <= current * per_page) {
                    layoutRefresh.setDirection(SwipyRefreshLayoutDirection.TOP);
                }
                dataArray.addAll(MyCardBean.dataChangeForBugCard(response.body().getData().getData()));
                adapter.notifyDataSetChanged();

            }
        });
    }

    public static void startActivity(Context context, String id) {
        Intent intent = new Intent(context, BuyCardListActivity.class);
        intent.putExtra(EXTRA_ID, id);
        context.startActivity(intent);
    }
}
